<template>
    <h1>商品添加</h1>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="fromdata" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
        <el-form-item label="商品名字" prop="name">
            <el-input v-model="fromdata.name" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
            <!-- <el-input v-model="fromdata.price" /> -->
            <el-input-number v-model="fromdata.price" :precision="2" :step="0.1" />
        </el-form-item>
        <el-form-item label="商品的标题" prop="subName">
            <el-input v-model="fromdata.subName" />
        </el-form-item>
        <el-form-item label="商品排序" prop="seq">
            <!-- <el-input v-model="fromdata.seq" /> -->
            <el-input-number v-model="fromdata.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="商品的描述" prop="brief">
            <el-input v-model="fromdata.brief" />
        </el-form-item>
        <el-form-item label="父目录ID" prop="categoryId">
            <!-- <el-input v-model="fromdata.categoryId" /> -->
             <!--级别选择器-->
            <el-tree-select v-model="fromdata.categoryId" :data="Categorydata" check-strictly :render-after-expand="false"
                style="width: 240px" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                提交
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { CategoryApi } from "@/api";
const fromdata = reactive({
    name: "",
    subName: '',
    price: 0,
    seq: 0,
    brief: "",
    categoryId:0
});
const ruleFormRef=ref()
const rules = reactive({
    name: [
        { required: true, message: '请输入商品的名称', trigger: 'blur' },
        { min: 2, max: 5, message: '必须使用字母或文字大于 2 小于 5字符', trigger: 'blur' },
    ],
})
const Categorydata = ref([
    {
        value: '1',
        label: '一级目录'
    }
])

onMounted(() => {
    CallCategoryApi()
})

const CallCategoryApi = () => { 
    CategoryApi.select.call().then((res:any) => {
        
        res.forEach((item:any) => {
            item.value = item.id
            item.label = item.name
        });
       
        console.log(res);
        const categoryData2 = res.reduce((hezi: any, currentValue: any) => { 
            currentValue.children = res.filter((item: any) => item.parentId === currentValue.id)
            if (currentValue.parentId === 0) { 
                hezi.push(currentValue)
            }
            return hezi
        }, [])
        console.log(categoryData2);
        Categorydata.value = categoryData2
     })
}


const submitForm = async (formEl:any) => {
    if (!formEl) return
    await formEl.validate((valid:any, fields:any) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl:any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>